home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / Borland Visual dBASE Professiona v7.0 / DATA1.CAB / Sample_dBASE / structure.prg < prev    next >
Encoding:
Text File  |  1997-11-20  |  5.1 KB  |  167 lines

  1. //------------------------------------------------------------------------
  2. //
  3. //  structure.prg -- class Structure
  4. //
  5. //  A custom class for working with structures passed to 
  6. //  external functions such as the Windows API. This 
  7. //  class is used by the Win32API sample form.
  8. //
  9. //  The following example shows how to create a two member
  10. //  structure and use the setMember() and getMember() methods
  11. //  to set and get the value of a structure member.
  12. /*
  13.  
  14.    se = new structExample()
  15.    se.setMember("memberOne", 128)
  16.    ? se.getMember("memberOne") 
  17.   
  18.    class structExample of Structure
  19.       super::addMember( TYPE_WORD,   "memberOne" )
  20.       super::addMember( TYPE_STRING, "memberTwo", 5)
  21.    endclass
  22.  
  23. */ 
  24. //
  25. //  Visual dBASE Samples Group
  26. //
  27. //  $Revision:   1.1  $
  28. //
  29. //  Copyright (c) 1997, Borland International, Inc. 
  30. //  All rights reserved.    
  31. //
  32. //------------------------------------------------------------------------  
  33.  
  34. #include "structapi.h"
  35.  
  36.  
  37.  
  38.  
  39. class Structure
  40.    local sFolder
  41.    sFolder = SUBSTR( PROGRAM(0), 1, ;
  42.              LEN( PROGRAM( 0 ) ) - LEN("structure.prg") )
  43.    DO (sFolder + "\STRUCTAPI.PRG" )
  44.    this.member    = new Array()
  45.    this.value = ""
  46.  
  47.    function addMember( nType, sName, nSize )
  48.       local i
  49.       nSize = IIF(PCOUNT()==3,nSize,0)
  50.       i = this.member.add( new StructMember( sName, nType, nSize ) )
  51.       if ( i > 1)
  52.          this.member[ i ].offset := this.member[ i - 1 ].offset + ;
  53.                                     this.member[ i - 1 ].size
  54.       endif   
  55.       this.value := this.value + ;
  56.          REPLICATE(" ", this.member[i].size)
  57.       class::setMember( sName )
  58.    return ( i )
  59.  
  60.    function getMember( sName )
  61.       local xValue, nBytes, i
  62.       xValue = null
  63.       nBytes = 0
  64.       i = class::findIndex( sName )
  65.  
  66.       if ( i > 0 )
  67.          nType   = this.member[i].type
  68.          nOffset = this.member[i].offset
  69.          nSize   = this.member[i].size
  70.          do case
  71.             case ( nType == TYPE_WORD   OR ;
  72.                    nType == TYPE_DWORD  OR ;
  73.                    nType == TYPE_LONG )
  74.                  xValue := GetStructNumber( this.value, ;
  75.                                             nOffset, ;
  76.                                             nType )
  77.             case ( nType == TYPE_STRING )
  78.                  xValue = REPLICATE(" ", nSize)
  79.                  nSize  = GetStructStringLen( this.value, nOffset )
  80.                  nBytes = GetStructString( this.value, ;
  81.                                            nOffset, ;
  82.                                            nSize, ;
  83.                                            xValue, ;
  84.                                            nSize)
  85.          endcase                     
  86.       endif
  87.    return ( xValue )
  88.  
  89.  
  90.    function setMember( sName, xValue )
  91.       local i, nBytes, nType, nOffset, nLength
  92.       i      = class::findIndex( sName )
  93.       nBytes = 0
  94.  
  95.       if ( i > 0 )
  96.          nType   = this.member[i].type
  97.          nOffset = this.member[i].offset
  98.          nSize   = this.member[i].size
  99.          nLength = this.length()
  100.          do case
  101.             case ( nType == TYPE_WORD OR ;
  102.                    nType == TYPE_DWORD OR ;
  103.                    nType == TYPE_LONG )
  104.                  xValue = IIF( PCOUNT() == 2, xValue, 0)
  105.                  nBytes := SetStructNumber( this.value, ;
  106.                                             nOffset, ;
  107.                                             nType, ;
  108.                                             nLength, ;
  109.                                             xValue )  
  110.             case ( nType == TYPE_STRING )
  111.                  if ( PCOUNT() == 2 )
  112.                     xValue = SUBSTR(xValue,1,nSize-1) + CHR(0)
  113.                  else
  114.                     xValue = REPLICATE(" ",nSize-1) + CHR(0)
  115.                  endif
  116.                  nBytes := SetStructString( this.value, ;
  117.                                             nOffset, ;
  118.                                             nLength, ;
  119.                                             xValue, ;
  120.                                             nSize )
  121.          endcase                     
  122.       endif
  123.    return ( xValue )
  124.  
  125.  
  126.    function findIndex( sName )
  127.       local i, bFound
  128.       bFound = false
  129.       for i = 1 to this.member.size
  130.           if ( this.member[i].name == sName )             
  131.              bFound = true
  132.              exit
  133.           endif          
  134.       next
  135.       if ( NOT bFound )
  136.          i := -1 
  137.        endif
  138.     return ( i )
  139.  
  140.    function length
  141.       local nLength
  142.       nLength = 0
  143.       for i = 1 to this.member.size
  144.           nLength = nLength + this.member[i].size
  145.       next
  146.    return nLength
  147.  
  148. endclass
  149.  
  150. class StructMember( sName, nType, nSize )
  151.    this.name   = sName
  152.    this.type   = nType
  153.    this.size   = 0
  154.    this.offset = 0
  155.    do case
  156.       case ( nType == TYPE_WORD )
  157.            this.size := SIZEOF_WORD
  158.       case ( nType == TYPE_DWORD )
  159.            this.size := SIZEOF_DWORD
  160.       case ( nType == TYPE_LONG )
  161.            this.size := SIZEOF_LONG
  162.       case ( nType == TYPE_STRING )
  163.            this.size := nSize
  164.    endcase
  165. endclass
  166.  
  167.